package com.xunshibao.acquiredata;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.junit.Test;

public class RenRenDownload {

    public RenRenDownload() {
        try {
            writer = new BufferedWriter(new FileWriter("deps_1.txt"));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Test
    public void downloadDept() {
        System.getProperties().put("proxyHost", "web-proxy.china.hp.com");
        System.getProperties().put("proxyPort", "8080");
        String url = "http://www.renren.com/GetDep.do?id=1001";
        try {
            // String body = Jsoup.connect(url).execute().body();
            // System.out.println(body);
            List<String> list = read();
            for (String id : list) {
                url = "http://www.renren.com/GetDep.do?id=" + id;
                try {
                    Document doc = Jsoup.connect(url).get();
                    Elements elements = doc.select("option");
                    Iterator<Element> itr = elements.iterator();
                    String value;
                    while (itr.hasNext()) {
                        value = itr.next().attr("value");
                        if (!"".equals(value)) {
                            write(id + '\t' + value + '\n');
                        }
                    }
                } catch (Exception e) {
                    System.out.println(id);
                }
            }
            writer.close();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Test
    public void test2() {
        try {
            read();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public List<String> read() throws Exception {
        List<String> list = new LinkedList<String>();
        String file = "E:/eone/workspace/mis/src/main/java/data.txt";
        // String file = this.getClass().getResource("data.txt").getFile();
        BufferedReader br = new BufferedReader(new FileReader(file));
        String line;
        while ((line = br.readLine()) != null) {
            list.add(line);
        }
        return list;
    }

    @Test
    public void test1() {
        char c = '阿';
        System.out.println((int) c);
        int i = 38463;
        System.out.println((char) i);
    }

    BufferedWriter writer;

    public void write(String line) {
        try {
            writer.append(line);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Test
    public void highschool() {
//        String url = "http://support.renren.com/highschool/{0}.html";
//        String url = "http://support.renren.com/collegeschool/{0}.html";
        String url = "http://support.renren.com/juniorschool/{0}.html";
        
        try {
            List<String> list = read();
            String url1;
            String body;
            for (String code : list) {
                url1 = url.replace("{0}", code);
                body = Jsoup.connect(url1).execute().body();
                writeFile(body, code);
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

//    File dir = new File("highschool");
//    File dir = new File("collegeschool");
    File dir = new File("juniorschool");
    public void writeFile(String content, String code) {
        if (!dir.exists()) {
            dir.mkdirs();
        }
        File file = new File(dir, code + ".txt");
        BufferedWriter bw = null;
        try {
            bw = new BufferedWriter(new FileWriter(file));
            bw.write(content);

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                bw.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    @Test
    public void exportHighSchool() {
        File[] files = dir.listFiles();
        Document doc;
        Elements elements;
        Iterator<Element> iter;
        Element ele;
        String code;
        String text;
        String ecode;
        File distinct = new File("distinct.txt");
//        File highschool = new File("collegeschool.txt");
//        File highschool = new File("juniorschool.txt");
        BufferedWriter disB = null;
//        BufferedWriter hiB = null;
        try {
            disB = new BufferedWriter(new FileWriter(distinct));
//            hiB = new BufferedWriter(new FileWriter(highschool));
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        int i = 0;
        for (File in : files) {
            i++;
            try {
                code = in.getName().substring(0, in.getName().lastIndexOf("."));
                code = judgeCode(code);
                doc = Jsoup.parse(in, "UTF-8");
                elements = doc.select("#schoolCityQuList a");
                if (elements != null && !elements.isEmpty()) {
                    iter = elements.iterator();
                    while (iter.hasNext()) {
                        ele = iter.next();
                        text = ele.text();// 锦江区
                        ecode = ele.attr("onclick");// SchoolComponent.tihuan('city_qu_510104')
                        ecode = extractCode(ecode);
                        if (ecode == null) {

                        } else {
                            disB.append(getPCode(code));
                            disB.append('\t');
                            disB.append(code);
                            disB.append('\t');
                            disB.append(ecode);
                            disB.append('\t');
                            disB.append(text);
                            disB.append('\n');

//                            exportAreaHighSchool(doc, ecode, hiB,code);
                        }

                    }
                }
//                break;
            } catch (IOException e) {
                System.out.println(in.getName());
            }
        }
        
        try {
            disB.close();
//            hiB.close();
        }catch(Exception e) {
            e.printStackTrace();
        }
        System.out.println("total" + i + " files processed.");
    }

    
    private String getPCode(String ccode) {
        return ccode.substring(0,2)+"00";
    }
    @Test
    public void test3() {
        System.out.println(getPCode("5334"));
    }
    private void exportAreaHighSchool(Document doc, String code, BufferedWriter writer,String ecode) {
        Elements elements = doc.select("#city_qu_" + code + " a");
        if (!elements.isEmpty()) {
            Iterator<Element> iter = elements.iterator();
            Element ele;
            try {
                while (iter.hasNext()) {
                    ele = iter.next();
                    writer.append(code);
                    writer.append('\t');
                    writer.append(ele.attr("href"));
                    writer.append('\t');
                    writer.append(ele.text());
                    writer.append('\n');
                    // ele.attr("href");//code
                    // ele.text();//text
                }
            } catch (Exception e) {
                System.out.println("fail to write code for :" + code);
            }
        } else {
            System.out.println("there is no school with code: " + code + ", ecode=" +ecode);
        }

    }

    Pattern ptn = Pattern.compile("SchoolComponent\\.tihuan\\('city_qu_(.*?)'\\)");

    public String extractCode(String str) {
        Matcher matcher = ptn.matcher(str);
        if (matcher.matches()) {
            return matcher.group(1);
        } else {
            System.out.println("unmatched string: " + str);
        }
        return null;
    }

    /**
     * 1100 3100 1200 5000 8100
     * 
     * @param code
     * @return
     */
    public String judgeCode(String code) {
        int value = Integer.valueOf(code);
        if (value == 1101 || value == 3101 || value == 1201 || value == 5001 || value == 8101) {
            return String.valueOf(value - 1);
        } else {
            return code;
        }
    }
}
